﻿// *************************************************************************************************
// Project: RealSense.Light.Core
// Namespace: RealSense.Light.Core
// File: SenseManager.cs  Author: Peter O'Hanlon
// Copyright © Peter O'Hanlon 2014-2015
// Date: 2015/1/21 07:05
// *************************************************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RealSense.Light.Core
{
  internal class SenseManagerHandler : ISenseManager
  {
    private bool initialised;
    private bool disposed;
    public void Create(PXCMSession session)
    {
      SenseManager = session.CreateSenseManager();
    }

    public void Initialise()
    {
      if (initialised) return;
      initialised = true;
      RealSenseBase.StaticInvokeAndThrow(()=>SenseManager.Init(), "initialising the sense manager");
    }

    public PXCMSenseManager SenseManager
    {
      get;
      private set;
    }

    public void StartProcessing()
    {
      while (RealSenseBase.StaticInvoke(()=>SenseManager.AcquireFrame(true)))
      {
        // We have a frame here, we can process it now.
      }
    }

    public void Dispose()
    {
      Dispose(true);
      GC.SuppressFinalize(this);
    }

    private void Dispose(bool disposing)
    {
      if (disposing && !disposed)
      {
        disposed = true;
        if (SenseManager != null)
        {
          SenseManager.Dispose();
        }
      }
    }
  }
}
